package server;

import common.Level;
import common.PlayerColor;
import common.PlayerDirection;
import common.PlayerStatus;


public class UpdateablePlayerImpl implements UpdateablePlayer {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -466003829924184591L;
	
	
	private int posX;
	private int posY;
	private int score;
	private PlayerStatus status;
	private PlayerColor color;
	private String name;
	private PlayerDirection direction;
	
	
	public UpdateablePlayerImpl(int posX, int posY){
		this.posX = posX;
		this.posY = posY;
		this.score = 0;
		this.status = PlayerStatus.ALIVE;
		this.direction = PlayerDirection.NoDirection;
	}
	

	@Override
	public int GetPosX() {
		return posX;
	}

	@Override
	public int GetPosY() {
		return posY;
	}

	@Override
	public PlayerStatus GetStatus() {
		return status;
	}
	
	@Override
	public void SetPlayerStatus(PlayerStatus s) {
		status = s;
	}

	@Override
	public int GetScore() {
		return score;
	}
	
	@Override
	public PlayerColor GetColor() {
		return color;
	}
	
	public void SetScore(int score) {
		this.score = score;
	}
	
	public void SetColor(PlayerColor c) {
		color = c;
	}
	
	public void SetPosition(int posX, int posY) {
		this.posX = posX;
		this.posY = posY;
	}

	
	@Override
	public void advancePlayer(Level level) {
		int newX = GetPosX();
		int newY = GetPosY();
		
		switch(direction) {
		case North: newY--;	break;
		case East:	newX++;	break;
		case South:	newY++;	break;
		case West:	newX--;	break;
		default: break;
		}
		
		if (level.canMoveTo(GetPosX(), GetPosY(), direction)) {
			SetPosition(newX, newY);
		}
	}
	
	
	public void moveUp( Level level){
		if( posY > 0 && !level.getField( posY-1, posX).equals( "X")){
			posY--;
		}
	}
	public void moveDown( Level level){
		if( posY < level.getRowAmount()-1 && !level.getField( posY+1, posX).equals( "X")){
			posY++;
		}
	}
	public void moveRight( Level level){
		if( posX < level.getColumnAmount()-1 && !level.getField( posY, posX+1).equals( "X")){
			posX++;
		}
	}
	public void moveLeft( Level level){
		if( posX > 0 && !level.getField( posY, posX-1).equals( "X")){
			posX--;
		}
	}
	
	public void setColor(PlayerColor c) {
		color = c;
	}
	
	public String toString() {
		return "player: " + name + ", " +
				(color == null ? "color (null)" : color.name()) + ", " +
				"score: " + score +	", " +
				"pos: " + posX + "," + posY;
	}

	@Override
	public String GetName() {
		return name;
	}

	@Override
	public void SetName(String playerName) {
		this.name = playerName;
	}


	@Override
	public PlayerDirection getDirection() {
		return direction;
	}


	@Override
	public void SetDirection(PlayerDirection d) {
		direction = d;
	}


	@Override
	public void earnCoin() {
			score++;
	}
}
